var profile_data   	 	= new Array();
var user_profile_arr 	= new Array();
var user_profile_ids	='';

jQuery(document).ready(function(){  
	jQuery('div.bubbleInfo').each(function () {
		activeMenu(this.id);
	});
	
	jQuery('div.profile_load').livequery("mouseover",function(){
		user_profile_ids = '';
		var user_id = this.lang;
		var cid 	= this.id;
		
		jQuery('div.bubbleInfo').each(function () {
			if(this.lang!='' &&  this.lang>0 &&!user_profile_arr[this.lang]){
				user_profile_arr[this.lang]=this.lang;
				
				user_profile_ids+=(user_profile_ids!=''?',':'')+''+this.lang+'';
			}
			else{
				this.lang = '';
			}
		});
		
		if(user_profile_ids!=''){
			var json_url = WEB_DIR+"ajax.php?act=user&code=mini_profile&user_ids="+user_profile_ids;
			
			if(jQuery.jCache.hasItem(json_url)){
				var data = eval(jQuery.jCache.getItem(json_url));
				
				if(data[user_id]){
					parse_miniprofile_data(data[user_id], cid);
				}
				
				for(i in data){
					profile_data[i] = data[i];
				}		
			}
			else{
				jQuery.post(WEB_DIR+"ajax.php?act=user&code=mini_profile",
						{user_ids:user_profile_ids},
							function(data){
								if(data[user_id]){
									parse_miniprofile_data(data[user_id], cid);
								}	
								jQuery.jCache.setItem(json_url, data);
								
								for(i in data){
									profile_data[i] = data[i];
								}
							}
							,"json"
							);
			}
		}
		
		if(profile_data[user_id]){
			parse_miniprofile_data(profile_data[user_id], cid);
		}
	});
});	

function activeMenu(info_id) {
    // options
    var distance = 5;
    var time = 5;
    var hideDelay = 5;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = jQuery('div.trigger', jQuery("#"+info_id));
    var popup   = jQuery('div.popup',   jQuery("#"+info_id)).css('opacity', 0);
	
	var commentAva 			= jQuery('div.commentAva', jQuery("#"+info_id));
	var mysiteCommentAva 	= jQuery('div.mysiteCommentAva', jQuery("#"+info_id));
	
	// set the mouseover and mouseout on both element
    jQuery([trigger.get(0), popup.get(0)]).mouseover(function () {														   
		//doi style border cho vien anh o trang viewpix
		commentAva.addClass("aHover");
		mysiteCommentAva.addClass("aMysiteHover");
	
      	// stops the hide event if we move from the trigger to the popup element
      	if (hideDelayTimer) clearTimeout(hideDelayTimer);

      	// don't trigger the animation again if we're being shown, or already visible
		if (beingShown || shown) {
			return;
		} 
		else{
			beingShown = true;
			// reset position of popup box
			popup.css({
			  top: 0, 
			  left: trigger.width()+7,
			  display: 'block' // brings the popup back in to view
			})
			// (we're using chaining on the popup) now animate it's opacity and position
			.animate({
			  top: '-=' + distance + 'px',
			  opacity: 1
			}, time, 'swing', function() {
			  // once the animation is complete, set the tracker variables
			  beingShown = false;
			  shown = true;
			});
		}
    }).mouseout(function () {
		// duc-nh: doi style border cho vien anh o trang viewpix
		commentAva.removeClass("aHover");
		mysiteCommentAva.removeClass("aMysiteHover");
		// reset the timer if we get fired again - avoids double animations
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
		// store the timer so that it can be cleared in the mouseover if required
		hideDelayTimer = setTimeout(function () {
			hideDelayTimer = null;
			popup.animate({top: '-=' + distance + 'px',opacity: 0}
						  , time, 'swing', function () {
															// once the animate is complete, set the tracker variables
															shown = false;
															// hide the popup entirely after the effect (opacity alone doesn't do the job)
															popup.css('display', 'none');
													   }
						  );
			}, hideDelay);
	});
}

function parse_miniprofile_data(data, cid){//cid comment id
	if(jQuery("#show_mini_profile_"+cid).html() == ''){
		mini_info = '';
		
  		if(data.full_name != '')
  			mini_info = mini_info + '<div class=\'miniLineHeight\'>Họ và tên: <strong>'+data.full_name+'</strong></div>';
		if(data.create_time != '')
  			mini_info = mini_info + '<div class=\'miniLineHeight\'>Ngày tham gia: <strong>'+data.create_time+'</strong></div>';
		if(data.address != '')
  			mini_info = mini_info + '<div class=\'miniLineHeight\'>Địa chỉ: <strong>'+data.address+'</strong></div>';
		if(data.mobile_phone != '')
  			mini_info = mini_info + '<div class=\'miniLineHeight\'>Liên hệ: <strong>'+data.mobile_phone+'</strong></div>';			
		if(data.yahoo_id != '')
  			mini_info = mini_info + '<div class=\'miniLineHeight\'>YM: <strong>'+data.yahoo_id+'</strong></div>';
		if(data.email != '')
  			mini_info = mini_info + '<div class=\'miniLineHeight\'>Email: <strong>'+data.email+'</strong></div>';
		if(data.website != '')
  			mini_info = mini_info + '<div class=\'miniLineHeight\'>Website: <strong>'+data.website+'</strong></div>';
			
		add_to_black_list = '<span class="get_blacklist" onclick="javascript: js_get_blacklist(\''+data.user_id+'|'+data.user_name+'\')" id="'+data.user_id+'|'+data.user_name+'"><a href="javascript: void(0)"><img src="style/images/icon_blacklist.gif" width="13" height="13"> Blacklist</a></span>';	
		
  		miniProfile = '<div class="contentPop" style="background-color:#fff"><div style="margin:4px;"><div class="blastTopLeft"><div class="blastTopRight"><div class="blastTopbg"><span></span></div></div></div><div class="blast">'+data.blast+'</div><div class="blastBottomLeft"><div class="blastBottomRight"><div class="blastBottombg"><span></span></div></div></div></div><div class="popupName"><a href="'+data.sender_link+'"><b>'+data.user_name+'</b></a>&nbsp;</div>'+mini_info+'<div class="miniIcon"><span class="floatLeft miniProfilePm"><a href="'+data.sender_link+'" onclick="mini_profile_pm(\''+data.user_id+'\',\''+data.user_name+'\')"><img src="style/images/pmIcon.gif" width="17" height="14" class="displayIn"> PM! </a> &nbsp;| &nbsp;</span><span class="floatLeft miniProfileFB"><a href=""'+data.sender_link+'"" onclick="mini_profile_fb(\''+data.user_id+'\',\''+data.user_name+'\')"><img src="style/images/writeIcon.gif" width="13" height="13" class="displayIn"> Viết lưu bút</a></span>&nbsp; | &nbsp;'+add_to_black_list+'</div><div style=" clear:left;height:1px; background-color:#CCCCCC"><span></span></div>'+data.top_item+'</div>';
		
		jQuery("#hide_mini_profile_"+cid).hide();
  		jQuery("#show_mini_profile_"+cid).html(miniProfile).show();
  	}
}

function show_luu_but_box(receipent, author, receive_name) {
	jQuery("#largepopup").css({width: "500px", height: "250px"}); //, background: "#FDFADE"
	jQuery("#largepopup_bg").css({width: "500px", height: "250px"});
	jQuery(".largepopup-footer").html("<a href='javascript:hideLargePopup(); ' class='close_ajax_box' title='Dong cua so'>X</a>");
	
	showLargePopup();
	// DRAW FORM POST LUU BUT BY AJAX -
	// VI MOI MOT FORM CO' author_id va receipend_id khac nhau nen moi lan click la phai xoa form cu va draw lai form moi
	if( jQuery("#mach_luu_but_box").html() != "" ) {
		jQuery("#mach_luu_but_box").html("");
	}
	
	draw_quick_comment_box(receipent, author, receive_name);
}

function hide_luu_but_box() {
	hideLargePopup();
	
	var largepopup_bg = document.getElementById('largepopup_bg');
	largepopup_bg.style.visibility = 'hidden';
}
//------------------ AJAX FUNCTOIN ----------------------------------------

function draw_quick_comment_box(receipent, author, receive_name) {
	var url = getValueId('url_ajax_box');
	jQuery('#loading').show();
	jQuery.ajax({
	   type: "POST",
	   url: url,
	   data: "receipent=" + receipent + "&author=" + author+"&receive_name="+receive_name,
	   success: function(msg){
		   jQuery("#mach_luu_but_box").html(msg);
		   jQuery('#loading').hide();
		   changeStyleSubmitBtt();
	   }
	 });	
}

function post_luu_but(data, id) {
	var url = getValueId('url_ajax');
	//alert(url); return false;
	var message = getValueId('message');
	var arr = new Array();
	arr["message"] = message;
	if (jQuery('#attachImage:checked').length==1)
		data += "&pix_url="+pix_url+"&pix_id="+pix_id;
		
	var check = check_luu_but_form(arr);
	if ( check == true ) {
		jQuery('#loading').show();
		jQuery('#luu_lai').hide();
		data = "message="+message+"&"+data;
		jQuery.ajax({
		   type: "POST",
		   url: url,
		   data: data,
		   success: function(msg){
			 jQuery("#form_luu_but").html("");
			 jQuery("#ajax_respone").html(msg);
			 jQuery('#loading').hide();
		   }
		 });
	}else {return false;}
}

function check_luu_but_form(arr) {
	var len = arr.length;
	if(arr["message"] == ""){
		alert("Vui lòng nhập nội dung cho lưu bút");
		return false;
	}	
	return true;
}

jQuery(".miniProfileFB").click(function(){
	if(!IS_LOGIN){
		login_error();
		return false;
	}
	else{
		arrData = this.id.split('|');
		blockSendFB(arrData[0],arrData[1]);
		return false;
	}
});
	
function mini_profile_pm(userid, username){
	if(!IS_LOGIN){
			login_error();
			return false;
	}
	else{	
		blockSendPm(userid,username);
		return false;
	}
}
	
function mini_profile_fb(userid, username){
	if(!IS_LOGIN){
		login_error();
		return false;
	}
	else{
		blockSendFB(userid,username);
		return false;
	}
}